function similarity_nonzero_eigs(file_name, num_movies, num_features, ...
  default_feature_value)
%
% Like similarity_eigs, but expects that file_name has been processed by
% bin/remove_zeros to remove the all-zero rows and cols. Here we add
% them back in. See src/remove_zeros.cpp for format info.
% NB: unlike similarity_eigs, the input matrix must be in one-row-per-line
% format.
%
% For the 17770 by 17770 matrix, this takes about 3GB of RAM.
%

% We need to chop off the last column if it's all zeros because of a trailing
% space in the matrix file.
S = dlmread(file_name, '');
if ~any(S(:,end))
  S = S(:,1:end-1);
end

% Load the zero row (and col, since input is symmetric) list.
% This should be a column vector.
% The first row (col) is numbered 0 in the file; we want it to be 1-based.
Z = load([file_name '.zeros']) + 1;

% We will later need the indexes of the non-zero entries, too.
NZ = setdiff([1:num_movies], Z)';

% Sanity check.
if size(Z, 1) + size(S, 1) ~= num_movies ...
  || size(NZ, 1) + size(Z, 1) ~= num_movies
  zero_entries = size(Z,1)
  nonzero_entries = size(S,1)
  num_movies
  error 'zeros and non-zeros do not add up to num_movies';
end

% Do PCA without zeros.
C = cov(S);
[V,D] = eigs(C, num_features);

% Augment V with defaults.
V_full = zeros(num_movies, num_features);
V_full(Z,:) = default_feature_value;
V_full(NZ,:) = V;

dlmwrite([file_name '.eigvals'], diag(D), ' ')
dlmwrite([file_name '.eigvecs'], V_full, ' ')
